********Figure 3 - Mexico********

*Load data
cd "$reploc/data/created_data"
use mex_full, clear

sort pid_link mxfls
drop if mxfls==.
keep if panel==1

keep rbuc* mxfls pid_link inflation imp_age panel birthyr grbrprovAvg_lam0 grbrprovSD_lam0 grbrprovAvg_lam0co? grbrprovSD_lam0co? entrep evermig smoke yearprov

reshape wide rbuc*  inflation imp_age panel grbrprovAvg_lam0 grbrprovSD_lam0 grbrprovAvg_lam0co? grbrprovSD_lam0co? entrep evermig smoke, i(pid_link) j(mxfls)

local varlist1 rbuc rbuc_nga inflation grbrprovAvg_lam0 grbrprovSD_lam0 grbrprovAvg_lam0co2 grbrprovSD_lam0co2 grbrprovAvg_lam0co3 grbrprovSD_lam0co3 entrep evermig smoke


foreach var in `varlist1'  {
gen `var'dif=.
replace `var'dif=`var'3-`var'2
}


*Generate cohort by birthyear variables to cluster on
gen coh_prov_clust=yearprov

*Figure
local x rbuc_nga
local rhsvari oldmexonly
local rhsvaruse _lam0co3dif
local usei grbr

drop if `x'dif==.

local outputvars smokedif evermigdif entrepdif

foreach var in `outputvars' {
local loc=strpos("`var'","dif")
local loc1=`loc'-1

local varib=substr("`var'",1,`loc1')

sum `varib'2, d
local mean`var'mex1=r(mean)
}

local color1rbuc dkgreen
local color2rbuc purple

reg `x'dif `usei'provAvg`rhsvaruse' `usei'provSD`rhsvaruse' inflationdif, cluster(yearprov)
predict `x'_pred if `x'dif!=. & `usei'provAvg`rhsvaruse'!=. & `usei'provSD`rhsvaruse'!=. & inflationdif!=. & yearprov!=.

*Sort predicted rbuc by sign
gen `x'predsignpos=.
replace `x'predsignpos=0 if `x'_pred<0 & `x'_pred!=.
replace `x'predsignpos=1 if `x'_pred>=0 & `x'_pred!=.

*Sort with respect to median within each sign 
gen `x'medsign=.
sum `x'_pred if `x'predsignpos==0, d
local medineg=r(p50)
sum `x'_pred if `x'predsignpos==1, d
local medipos=r(p50)

*Sort by sign and median
replace `x'medsign=1 if `x'predsignpos==0 & `x'_pred<`medineg'
replace `x'medsign=2 if `x'predsignpos==0 & `x'_pred>=`medineg'
replace `x'medsign=3 if `x'predsignpos==1 & `x'_pred<`medipos'
replace `x'medsign=4 if `x'predsignpos==1 & `x'_pred>=`medipos'
sort `x'medsign

local i=0

foreach varib in `outputvars' {
local i=`i'+1

reg `varib' i.`x'predsignpos, vce(bootstrap, reps(100)) 

matrix h`varib'`pi'dum = r(table)
gen p`i'=h`varib'`pi'dum[4,2]
gen lb`i'=.
gen ub`i'=.
gen mean`i'=.

forval pi=1(1)4 {
reg `varib' if `x'medsign==`pi', vce(bootstrap, reps(100))
replace mean`i'=_b[_cons] if `x'medsign==`pi'

matrix h`varib'`pi' = r(table)

replace lb`i'=h`varib'`pi'[5,1] if `x'medsign==`pi'
replace ub`i'=h`varib'`pi'[6,1] if `x'medsign==`pi'
} 
}

*Now that have mean, lb, ub, can keep one observation
keep `x'medsign lb* ub* p* mean*
duplicates drop `x'medsign, force
reshape long lb ub mean p, i(`x'medsign) j(variable)

gen variablename=""
local i=0
foreach varib in `outputvars' {
local i=`i'+1
replace variablename="`varib'" if variable==`i'
}

gen locat=.

forval pi=1(1)4 {
replace locat=`pi' if `x'medsign==`pi'
}

capture label drop locatlab
label define locatlab 1 "<0 & Below Median" 2 "<0 & Above Median" 3 ">0 & Below Median" 4 ">0 & Above Median"

foreach varib in `outputvars' {

levelsof variablename, local(levs)

foreach l in `levs' {
sum p if variablename=="`l'"
local p`l'mex=round(r(mean),.001)
local mean`varib'mex2=round(`mean`varib'mex1',.001)


if `p`l'mex'==0 {
local p`l'mex=0.0001
}

}

label values locat locatlab

if "`varib'"=="entrepdif" {
local tit "{&Delta} Self-Employment"
local numplpi=0.17
local numplpi2=0.16
local maxy=0.18
local xval1=2.5
}

if "`varib'"=="evermigdif" {
local tit "{&Delta} Migrated"
local numplpi=0.032
local numplpi2=0.03
local maxy=0.035
local xval1=2.5
}

if "`varib'"=="smokedif" {
local tit "{&Delta} Smoking"
local numplpi=0.115
local numplpi2=0.11
local maxy=0.12
local xval1=2.5
}


twoway (bar mean locat if `x'medsign==1 & variablename=="`varib'", color(`color1rbuc'*1)) (bar mean locat if `x'medsign==2 & variablename=="`varib'", color(`color1rbuc'*0.5)) (bar mean locat if `x'medsign==3 & variablename=="`varib'", color(`color2rbuc'*0.5)) (bar mean locat if `x'medsign==4 & variablename=="`varib'", color(`color2rbuc'*1)) (rcap ub lb locat if variablename=="`varib'", lcolor(gs8)) ///
, graphregion(color(white)) xlabel(1 2 3 4, valuelabel labsize(small) noticks) xtitle(" ") ///
legend(off) ///
text(`numplpi' 2.5 "P-value of difference (<0 vs. >0): `p`varib'mex'", place(c)  size(medsmall))  ///
text(`numplpi2' 2.5 "Mean in MxFLS2: `mean`varib'mex2'", place(c)  size(medsmall))  ///
subtitle(`tit', position(11) justification(left) size(medsmall)) `addsom' ysc(r(`maxy')) ///


cd "$reploc/results"
graph export Figure3Mexico`varib'.eps, replace

}


